package com.example.locationtracking.view;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.locationtracking.R;
import com.example.locationtracking.service.LocationUpdateReceiver;
import com.example.locationtracking.utils.Const;

public abstract class BaseActivity extends FragmentActivity {
	protected ProgressDialog progressDlg;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initUI();
	}
	protected abstract void initUI();
	public void showProgressDlg(){
		if(progressDlg==null)
			progressDlg = ProgressDialog.show(this,getString(R.string.app_name_txt).toUpperCase(), getString(R.string.loading), true, true);
		else
			progressDlg.show();
	}
	public void dismissProgressDlg(){
		try{
			if(progressDlg!=null && progressDlg.isShowing()){
				progressDlg.dismiss();
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void showNetworkErrorDlg(){
		Intent intent = new Intent(this,NetWorkErrorActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivityForResult(intent, Const.NETWORK_ACTIVITY_REQUEST_CODE);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == Const.NETWORK_ACTIVITY_REQUEST_CODE) {
	    	if(resultCode == RESULT_OK){
	    		//this.recreate();
	    		//this.finish();
	    	}
	    	else if(resultCode == Const.EXIT_KEY)
	    	{
	    		this.finish();
	    	}
	    }
	}
	
	public void onResume(){
		super.onResume();
	}
	protected void showLocationSettingDlg(int resString){
		
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage(getResources().getString(resString));
            dialog.setPositiveButton(getResources().getString(R.string.msg_open_location_setting), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    // TODO Auto-generated method stub
                    Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
                    startActivity(myIntent);
                    finish();
                }
            });          
            dialog.show();
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.logOut:
	        	AlertDialog.Builder dlgBuilder = new Builder(this).setMessage(R.string.msg_exit)
	        			.setPositiveButton("OK",new DialogInterface.OnClickListener() {
	        		        public void onClick(DialogInterface dialog, int whichButton) {
	        		        	Intent alarmIntent = new Intent(BaseActivity.this, LocationUpdateReceiver.class);
	        		    		PendingIntent pendingIntent = PendingIntent.getBroadcast(BaseActivity.this, 0,
	        		    				alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
	        		    		AlarmManager alarmManager = (AlarmManager)BaseActivity.this
	        		    				.getSystemService(Context.ALARM_SERVICE);
	        		    		alarmManager.cancel(pendingIntent);
	        		    		
	        		    		//exit app
	        		    		Intent intent = new Intent(BaseActivity.this, SplashActivity.class);
	        		    	    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        		    	    //intent.putExtra(Const.EXIT_APP, true);
	        		    	    startActivity(intent);
	        		    	    finish();        	
	        		        }
	        		   })
	        			.setNegativeButton("Cancel",null)
	        			.setCancelable(false);
	        	dlgBuilder.show();
	            return true;
	        
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}
